﻿
Imports std
Imports managedServer
Imports lpmsengi
Imports cacheProcessor

Public Module lpmsserv

    Public Const defaultConfigfile As String = "lpmsserv.ini"

    Private Sub shutdown()
        lpmsengi.finalize()
    End Sub

    Public Sub main(ByVal args() As String)
        debugpause()
        AddHandler ProHttpS.configChanged, AddressOf initial
        AddHandler ProHttpS.initial, AddressOf initial
        AddHandler ProHttpS.serviceshutdown, AddressOf shutdown
        ProHttpS.defaultConfigfile = defaultConfigfile
        'seems i should try to use storoomTalks here
        ProHttpS.startServer(args, contextParameter:=New httpContext.parameterDefinition(stringsPackage:=sp))
    End Sub

    Public Class processorConfig
        Public s As String
        Public path As String
        Public cache As Int64
        Public cacheType As cachetypeEnum
    End Class

    'script0 = scriptfile,path,cache 
    Private Function parseScriptline(ByVal s As String, ByRef pc As processorConfig) As Boolean
        Dim words As vector(Of String) = Nothing
        strsepar(s, character.comma, words)
        If words Is Nothing OrElse words.size() < 1 Then
            Return False
        Else
            pc = New processorConfig()
            pc.s = words(0)
            pc.path = ifoper(words.size() = 1, network.pathSeperator + words(0), words(1))
            kickLeftRightBlank(pc.path)
            If words.size() < 2 OrElse Not convertor.toint64(words(2), pc.cache) Then
                pc.cache = 0
            End If
            If words.size() = 4 Then
                If strsame(words(3), "none") Then
                    pc.cacheType = cachetypeEnum.none
                ElseIf strsame(words(3), "urlfull") Then
                    pc.cacheType = cachetypeEnum.urlfull
                ElseIf strsame(words(3), "pathquery") Then
                    pc.cacheType = cachetypeEnum.pathquery
                Else
                    pc.cacheType = cachetypeEnum.lang_urlfull
                End If
            End If

            Return True
        End If
    End Function

    Private Delegate Function createRequestProcessorDelegate(ByVal pc As processorConfig) As Global.requestProcessor

    Private Function createScriptRequestProcessor(ByVal pc As processorConfig) As Global.requestProcessor
        assert(Not pc Is Nothing, "processorConfig is nothing.")
        If pc.cache <> 0 Then
            Return New scriptCacheProcessor(pc.path, pc.cache, pc.s, pc.cacheType)
        Else
            Return New scriptRequestProcessor(pc.path, pc.s)
        End If
    End Function

    Private Function createNameRequestProcessor(ByVal pc As processorConfig) As Global.requestProcessor
        assert(Not pc Is Nothing, "processorConfig is nothing.")
        If pc.cache <> 0 Then
            Return New nameCacheProcessor(pc.s, pc.path, pc.cache, pc.cacheType)
        Else
            Return New nameRequestProcessor(pc.s, pc.path)
        End If
    End Function

    Private Function createRequestProcessor(ByVal script As String, ByRef rp As Global.requestProcessor, _
                                            ByVal create As createRequestProcessorDelegate, _
                                            Optional ByVal defaultscript As Boolean = False) As Boolean
        assert(Not create Is Nothing)
        Dim pc As processorConfig = Nothing
        If parseScriptline(script, pc) Then
            If defaultscript Then
                pc.path = network.pathSeperator
            End If
            rp = create(pc)
            If Not rp Is Nothing Then
                Return True
            Else
                raiseError("cannot create requestProcessor for script " + script + ", ignore.", _
                           errorHandle.errorType.warning)
                Return False
            End If
        Else
            raiseError("cannot parse script line " + script + ", ignore.", errorHandle.errorType.warning)
            Return False
        End If
    End Function

    Private Sub insertScriptRequestProcessor(ByVal script As String, Optional ByVal defaultscript As Boolean = False)
        Dim rp As Global.requestProcessor = Nothing
        If createRequestProcessor(script, rp, AddressOf createScriptRequestProcessor, defaultscript) Then
            assert(Not rp Is Nothing, "rp is nothing.")
            ProHttpS.insertRequestProcessor(rp)
        End If
    End Sub

    Private Sub insertNameRequestProcessor(ByVal script As String, Optional ByVal defaultscript As Boolean = False)
        Dim rp As Global.requestProcessor = Nothing
        If createRequestProcessor(script, rp, AddressOf createNameRequestProcessor, defaultscript) Then
            assert(Not rp Is Nothing, "rp is nothing.")
            ProHttpS.insertRequestProcessor(rp)
        End If
    End Sub

    Private Sub initial(ByVal config As configMonitor)
        ProHttpS.managedRequestProcessors().clear()
        ProHttpS.insertRequestProcessor(New storoomDelegate())
        lpmsengi.initial(config, configNaming.lpmsserv.name, False)
        Dim scripts As vector(Of String) = Nothing
        If config.readlist(configNaming.lpmsserv.name, configNaming.lpmsserv.script, scripts) AndAlso _
           Not scripts Is Nothing AndAlso scripts.size() > 0 Then
            Dim i As Int64
            For i = 0 To scripts.size() - 1
                insertScriptRequestProcessor(scripts(i))
            Next
        Else
            raiseError("cannot read script list from configuration.", errorHandle.errorType.warning)
        End If

        Dim names As vector(Of String) = Nothing
        If config.readlist(configNaming.lpmsserv.name, configNaming.lpmsserv.nameScript, names) AndAlso _
           Not names Is Nothing AndAlso names.size() > 0 Then
            For i As Int64 = 0 To names.size() - 1
                insertNameRequestProcessor(names(i))
            Next
        Else
            raiseError("cannot read name list from configuration.", errorHandle.errorType.warning)
        End If

        Dim defaultscript As String = Nothing
        If config.getparameter(configNaming.lpmsserv.name, configNaming.lpmsserv.default, defaultscript) Then
            insertScriptRequestProcessor(defaultscript, True)
        End If
    End Sub
End Module

Namespace configNaming
    Namespace lpmsserv
        Public Module lpmsserv
            Public Const name As String = "lpmsserv"
            Public Const script As String = "script"
            Public Const nameScript As String = "name"
            Public Const [default] As String = "default"
        End Module
    End Namespace

    Namespace defaultValue
        Namespace lpmsserv
            Public Module lpmsserv
                Public Const scriptpath As String = "script"
            End Module
        End Namespace
    End Namespace
End Namespace
